/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ins.web;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.UploadUtils;
import com.thinkgem.jeesite.common.utils.XMLUtil;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ins.entity.InsTable;
import com.thinkgem.jeesite.modules.ins.entity.InsTemplate;
import com.thinkgem.jeesite.modules.ins.service.InsTemplateService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;

/**
 * 模板管理Controller
 * @author fanqinglong
 * @version 2017-11-21
 */
@Controller
@RequestMapping(value = "${adminPath}/ins/insTemplate")
public class InsTemplateController extends BaseController {

	@Autowired
	private InsTemplateService insTemplateService;
	
	@ModelAttribute
	public InsTemplate get(@RequestParam(required=false) String id) {
		InsTemplate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = insTemplateService.get(id);
		}
		if (entity == null){
			entity = new InsTemplate();
		}
		return entity;
	}
	
	@RequiresPermissions("ins:insTemplate:view")
	@RequestMapping(value = {"list", ""})
	public String list(InsTemplate insTemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<InsTemplate> page = insTemplateService.findPage(new Page<InsTemplate>(request, response), insTemplate); 
		model.addAttribute("page", page);
		return "modules/ins/insTemplateList";
	}
	
	//@RequiresPermissions("ins:insTemplate:view")
	@RequestMapping(value = {"bindList"})
	public String bindList(InsTemplate insTemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
		String tableId = request.getParameter("tableId");
		Page<InsTemplate> page = insTemplateService.findPage(new Page<InsTemplate>(request, response), insTemplate); 
		model.addAttribute("page", page);
		model.addAttribute("tableId", tableId);
		return "modules/ins/insBindTemplateList";
	}

	@RequiresPermissions("ins:insTemplate:view")
	@RequestMapping(value = "form")
	public String form(InsTemplate insTemplate, Model model) {
		model.addAttribute("insTemplate", insTemplate);
		return "modules/ins/insTemplateForm";
	}

	@RequiresPermissions("ins:insTemplate:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("file") CommonsMultipartFile file,InsTemplate insTemplate, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request,HttpServletResponse response) {
		if (!beanValidator(model, insTemplate)){
			return form(insTemplate, model);
		}
		response.setContentType("text/html;charset=UTF-8");
		if (!file.isEmpty()) {
			String originName = file.getOriginalFilename();
			String[] info = new UploadUtils().uploadFile(request,file,"template");
			if ("true".equals(info[0])) {
				String src = info[4].replace("/gxptool", "");
				String newPath = "/"+new Date().getTime()+".ftl";
				String systemPath = request.getSession().getServletContext().getRealPath("/");
				File file2 = new File(systemPath+src);
				// 复制文件
				//FileUtils.copyFile(systemPath+src, systemPath+newPath);
				String templatePath = DictUtils.getDictValue("模板路径", "templatePath", "模板路径");;
				if (StringUtils.isBlank(templatePath)) {
					templatePath = "d:\\gxptool_template\\";
				}
				XMLUtil.parse(file2, templatePath+newPath);
				//删除原来的
				File oldFile = new File(templatePath+insTemplate.getSrc());
				if (oldFile.exists()) {
					oldFile.delete();
				}
				insTemplate.setSrc(newPath);
				insTemplate.setOriginName(originName);
				insTemplateService.save(insTemplate);
				addMessage(redirectAttributes, "保存模板成功");
				return "redirect:"+Global.getAdminPath()+"/ins/insTemplate/?repage";
			}else {
				addMessage(redirectAttributes, info[0]);
				return "redirect:"+Global.getAdminPath()+"/ins/insTemplate/?repage";
			}
		}
		addMessage(redirectAttributes, "文件为空！");
		return "redirect:"+Global.getAdminPath()+"/ins/insTemplate/?repage";
	}
	
	@RequiresPermissions("ins:insTemplate:edit")
	@RequestMapping(value = "delete")
	public String delete(InsTemplate insTemplate, RedirectAttributes redirectAttributes) {
		insTemplateService.delete(insTemplate);
		addMessage(redirectAttributes, "删除模板成功");
		return "redirect:"+Global.getAdminPath()+"/ins/insTemplate/?repage";
	}

	@RequiresPermissions("ins:insTemplate:edit")
	@RequestMapping(value = "getByName")
	@ResponseBody
	public String getByName(InsTemplate insTemplate){
		String result = "false";
		InsTemplate resultTemplate = insTemplateService.getByName(insTemplate.getName());
		if (null == resultTemplate || resultTemplate.getId().equals(insTemplate.getId())) {
			result = "true";
		}
		return result;
	}
	
	@RequiresPermissions("ins:insTemplate:edit")
	@RequestMapping(value = "validDelete")
	@ResponseBody
	public String validDelete(InsTemplate insTemplate){
		List<InsTable> results = insTemplateService.validDelete(insTemplate.getId());
		if (null != results && results.size() > 0) {
			return "false";
		}
		return "true";
	}
}